/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.piwicode.bywordofmouth.ui;

import com.googlecode.cqengine.IndexedCollection;
import com.googlecode.cqengine.query.QueryFactory;
import com.googlecode.cqengine.resultset.ResultSet;
import org.piwicode.bywordofmouth.FileResource;
import org.piwicode.bywordofmouth.Resource;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.ExpandVetoException;
import javax.swing.tree.TreeModel;

/**
 *
 * @author pierre
 */
public class SimpleTree extends javax.swing.JFrame {

    private final IndexedCollection<Resource> resourceIndexed;
    private final DefaultMutableTreeNode root;
    private final TreeWillExpandListener expandListener = new TreeWillExpandListener() {
        @Override
        public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
            final DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) event.getPath().getLastPathComponent();
            createChilds(dmtn);
        }

        @Override
        public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
        }
    };

    final void createChilds(final DefaultMutableTreeNode dmtn) {
        final Resource resource = (Resource) dmtn.getUserObject();
        final ResultSet<Resource> rs = resourceIndexed.retrieve(QueryFactory.equal(FileResource.PARENT, resource));
        for (final Resource r : rs) {
            final DefaultMutableTreeNode child = new DefaultMutableTreeNode(r);
            child.setAllowsChildren(r.isDirectory());
            dmtn.add(child);
        }
    }

    /**
     * Creates new form SimpleTree
     */
    public SimpleTree(Resource root, IndexedCollection<Resource> resourceIndexed) {
        this.resourceIndexed = resourceIndexed;
        this.root = new DefaultMutableTreeNode(root);
        this.root.setAllowsChildren(true);
        createChilds(this.root);
        initComponents();
        jTree1.addTreeWillExpandListener(expandListener);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTree1.setModel(createTreeModel());
        jScrollPane1.setViewportView(jTree1);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public static void go(final Resource root, final IndexedCollection<Resource> resourceIndexed) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SimpleTree(root, resourceIndexed).setVisible(true);
            }
        });
    }

    TreeModel createTreeModel() {
        return new DefaultTreeModel(root, true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables
}
